package com.example.shushiyunsst.fragment.main.home.child

import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.utils.KeyboardUtils
import com.example.library_common.view.web_view.JssAdvancedWebView
import com.example.shushiyunsst.R
import com.example.shushiyunsst.util.INCUBATOR
import com.example.shushiyunsst.util.WEB_TITLE
import com.example.shushiyunsst.util.WEB_URL

/**
 * 孵化
 * */
@Route(path = INCUBATOR)
class IncubatorFragment : BaseFragmentEx(), JssAdvancedWebView.Listener {

    companion object {
        fun newInstance(): IncubatorFragment {
            val args = Bundle()

            val fragment = IncubatorFragment()
            fragment.arguments = args
            return fragment
        }
    }

    @JvmField
    @Autowired(name = WEB_URL)
    var url = "http://hatch.regyun.com"

    @JvmField
    @Autowired(name = WEB_TITLE)
    var web_title = ""

    private var isReady = false

    private var mToolbar: Toolbar? = null

    private lateinit var mReloading: TextView

    private var mJssAdvancedWebView: JssAdvancedWebView? = null

    private lateinit var mProgressBar: ProgressBar


    override fun getLayoutId() = R.layout.incubator_fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mToolbar = view.findViewById(R.id.mToolbar)
        mToolbar?.title = web_title
        mToolbar?.setNavigationOnClickListener { pop() }

        mReloading = view.findViewById(R.id.mReloading)

        mJssAdvancedWebView = view.findViewById(R.id.mWebView)

        mProgressBar = view.findViewById(R.id.mProgressBar)



        mReloading.setOnClickListener {
            mJssAdvancedWebView?.reload()
            mReloading.visibility = View.GONE
        }

        initWebView()

        mJssAdvancedWebView?.loadUrl(url)
    }

    private fun initWebView() {
        mJssAdvancedWebView?.setListener(this, this)

        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
            val settings = mJssAdvancedWebView?.settings
            settings?.cacheMode = WebSettings.LOAD_NO_CACHE
            settings?.domStorageEnabled = true
            settings?.javaScriptEnabled = true
            settings?.blockNetworkImage = false
            settings?.javaScriptCanOpenWindowsAutomatically = true
            settings?.loadsImagesAutomatically = true
            settings?.allowFileAccess = true
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                settings?.allowUniversalAccessFromFileURLs = true
                settings?.allowFileAccessFromFileURLs = true
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                settings?.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            }
        }

        mJssAdvancedWebView?.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress == 100) {
                    mProgressBar.visibility = View.GONE
                } else {
                    mProgressBar.visibility = View.VISIBLE
                    mProgressBar.progress = newProgress
                }
            }
        }

        mJssAdvancedWebView?.webViewClient = object : WebViewClient() {

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                isReady = false
                mProgressBar.visibility = View.VISIBLE
                mProgressBar.progress = 0
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                isReady = true

            }
        }
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        mJssAdvancedWebView?.onResume()
    }


    override fun onResume() {
        super.onResume()
        mJssAdvancedWebView?.onResume()
    }

    override fun onBackPressedSupport(): Boolean {
        if (mJssAdvancedWebView?.canGoBack() == true) {
            mJssAdvancedWebView?.goBack()
            return true
        }
        return super.onBackPressedSupport()
    }

    override fun onPause() {
        super.onPause()
        mJssAdvancedWebView?.onPause()

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    override fun onDestroy() {
        super.onDestroy()

        mJssAdvancedWebView?.webChromeClient = null

        mJssAdvancedWebView?.onDestroy()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mJssAdvancedWebView?.onActivityResult(requestCode, resultCode, data)
    }

    override fun onPageStarted(url: String?, favicon: Bitmap?) {

    }

    override fun onPageFinished(url: String?) {

    }

    override fun onPageError(errorCode: Int, description: String?, failingUrl: String?) {
        mReloading.visibility = View.VISIBLE
    }

    override fun onDownloadRequested(
        url: String?,
        suggestedFilename: String?,
        mimeType: String?,
        contentLength: Long,
        contentDisposition: String?,
        userAgent: String?
    ) {

    }

    override fun onExternalPageRequest(url: String?) {

    }

}